/**
 * 20071226 1325: Eric Kao was created.
 */

package basic;

import java.io.IOException;

import javax.servlet.ServletException;

import org.apache.log4j.Logger;
import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;

import com.e885.log.E885Logger;

/**
 * DWR HelloWorld, 簡單的測試程式<br>
 * <br>
 * 注意:<br>
 * 1. 不淮許使用 overload, 因為使用 overload, 會造成呼叫第一個 method時, 會沒有任何反應<br>
 * <br>
 * Copyright: Copyright (c) 2001-2007<br>
 * Company: E885 Group
 * 
 * @author Eric.Kao(gau168.gau@msa.hinet.net)
 * @version 1.0
 */
public class BasicUtil
{
	public BasicUtil()
	{
		E885Logger.testLog.debug( "Creat default constructor" );
	}

	/**
	 * 取得一個字串<br>
	 * <br>
	 * 使用於 basic.jsp and session.jsp
	 * 
	 * @param name
	 * @return
	 */
	public String getHelloName( String name )
	{
		E885Logger.testLog.debug( "name:" + name );

		return "Hello:" + name;
	}

	/**
	 * 取得 hello name , 並設定 delay time<br>
	 * <br>
	 * 使用於 loadingMessage.jsp
	 * 
	 * @param name hello name
	 * @param time delay name
	 * @return display string
	 */
	public String getHelloNameDelay( String name, long time )
	{
		E885Logger.testLog.debug( "name:" + name + ", time:" + time );

		try
		{
			Thread.sleep( time * 1000 );
		}
		catch( InterruptedException e )
		{
			e.printStackTrace();
		}

		return "Hello:" + name;
	}

	/**
	 * Fetch a resource using forwardToString()<br>
	 * <br>
	 * 使用於 forward.jsp and forward/forward.html.
	 * 
	 * @return a demo HTML page
	 * @throws ServletException If the servlet engine breaks
	 * @throws IOException If the servlet engine breaks
	 */
	public String getInclude()
		throws ServletException, IOException
	{
		E885Logger.testLog.debug( "execute here" );

		WebContext context = WebContextFactory.get();
		return context.forwardToString( "/basic/forward/forward.html" );
	}

	/**
	 * 測試 Exception 用<br>
	 * <br> 
	 * 使用於 exception.jsp
	 * 
	 * @throws MyException
	 */
	public void testException()
		throws MyException
	{
		E885Logger.testLog.debug( "execute here" );

		try
		{
			throw new IllegalStateException( "It's Error" );
		}
		catch( IllegalStateException ex )
		{
			throw new MyException( "顯示錯誤訊息:" + ex.getMessage() );
		}
	}
}
